﻿using System;
using System.IO;

namespace LiveNet.Silverlight
{
    /// <summary>
    /// 
    /// </summary>
    public class MediaFile
    {
        private readonly string m_FileName;
        private readonly string m_MimeType;
        private readonly int m_ByteSize;
        private readonly MediaFileSet m_FileSet;
        private readonly Uri m_FileUrl;

        internal MediaFile(string filename, string mimeType, int byteSize, MediaFileSet fileSet)
        {
            if (string.IsNullOrEmpty(filename))
            {
                throw new ArgumentNullException("filename");
            }

            if (string.IsNullOrEmpty(mimeType))
            {
                throw new ArgumentNullException("mimeType");
            }

            m_FileSet = fileSet;
            m_FileName = filename;
            m_MimeType = mimeType;
            m_ByteSize = byteSize;
            m_FileUrl = new Uri(
                string.Format("https://silverlight.services.live.com/{0}/{1}/{2}", m_FileSet.AccountId,
                              m_FileSet.Name, FileName));
        }

        #region Public Properties
        /// <summary>
        /// Gets the name of the file.
        /// </summary>
        /// <value>The name of the file.</value>
        public string FileName
        {
            get { return m_FileName; }
        }

        /// <summary>
        /// Gets the MIME type.
        /// </summary>
        /// <value>The MIME type.</value>
        public string MimeType
        {
            get { return m_MimeType; }
        }

        /// <summary>
        /// Gets the bytesize.
        /// </summary>
        /// <value>The bytesize.</value>
        public int ByteSize
        {
            get { return m_ByteSize; }
        }

        /// <summary>
        /// Gets the file URL.
        /// </summary>
        /// <value>The file URL.</value>
        public Uri FileUrl
        {
            get { return m_FileUrl; }
        }
        #endregion

        #region Public Methods
        /// <summary>
        /// Gets the file stream.
        /// </summary>
        /// <returns>The file as a stream which can then be saved or manipulated as needed.</returns>
        public Stream GetFileStream()
        {
            if (m_FileSet.HasBeenDeleted)
            {
                throw new LiveNetException("GetFileStream");
            }
            return m_FileSet.m_LiveMedia.GetFileStream(FileUrl, MimeType);
        }
        #endregion
    }
}
